
# -*- coding:utf-8 -*-
import logging
# import requests
# import json
import sys
from .drivers.pixels import Pixels
# import re
# import logging
from . import plugin_loader
from . import config
import logging


reload(sys)


_logger = logging.getLogger(__name__)



def on_ready(mic,wxbot=None,pixels=None):

    for plugin in plugin_loader.get_plugins_ready():
        try:
            plugin.onReady(mic, config.get(), wxbot, pixels)
        except Exception:
            _logger.error("plugin '%s' run error",
                          plugin.__name__, exc_info=True)


def kwdActivity(mic, wxbot=None,pixels=None):

    for plugin in plugin_loader.get_plugins_wakeUp():
        # continueHandle = False
        try:
            plugin.wakeUp(mic, config.get(), wxbot,pixels)
        except Exception:
            _logger.error("plugin '%s' run error",
                          plugin.__name__, exc_info=True)
        # finally:
            # if not continueHandle:
            #     break


def beforeListen(mic, wxbot=None,pixels=None):
    # run plugins before listen
    for plugin in plugin_loader.get_plugins_before_listen():
        try:
             plugin.beforeListen(mic, config.get(), wxbot,pixels)
        except Exception:
            _logger.error("plugin '%s' run error",
                          plugin.__name__, exc_info=True)




def afterListen(mic, wxbot=None,pixels=None):
    # run plugins after listen
    for plugin in plugin_loader.get_plugins_after_listen():
        # continueHandle = False
        try:
            plugin.afterListen(mic, config.get(), wxbot,pixels)
        except Exception:
            _logger.error("plugin '%s' run error",
                               plugin.__name__, exc_info=True)
        # finally:
            # if not continueHandle:
            #     break

def beforeSay(mic, wxbot=None,pixels=None):
    for plugin in plugin_loader.get_plugins_before_say():
        # continueHandle = False
        try:
            plugin.beforeSay(mic, config.get(), wxbot, pixels)
        except Exception:
            _logger.error("plugin '%s' run error",
                          plugin.__name__, exc_info=True)
        # finally:
        #     if not continueHandle:
        #         break

def afterSay(mic, wxbot=None, pixels=None):
    for plugin in plugin_loader.get_plugins_after_say():
        # continueHandle = False
        try:
            plugin.afterSay(mic, config.get(), wxbot, pixels)
        except Exception:
            _logger.error("plugin '%s' run error",
                          plugin.__name__, exc_info=True)
        # finally:
        #     if not continueHandle:
        #         break

def onThink(mic, wxbot=None, pixels=None):
    for plugin in plugin_loader.get_plugins_onThink():
        # continueHandle = False
        try:
            plugin.onThink(mic, config.get(), wxbot, pixels)
        except Exception:
            _logger.error("plugin '%s' run error",
                          plugin.__name__, exc_info=True)
        # finally:
        #     if not continueHandle:
        #         break

def off(mic, wxbot=None, pixels=None):
    for plugin in plugin_loader.get_plugins_off():
        # continueHandle = False
        try:
            plugin.off(
                mic, config.get(), wxbot, pixels)
        except Exception:
            _logger.error("plugin '%s' run error",
                          plugin.__name__, exc_info=True)
        # finally:
        #     if not continueHandle:
        #         break

